
<template>
    <el-form ref="form" :model="data" :rules="rules" label-width="100px">
    <el-form-item :label="'昵称'" prop="nickname">
      <el-input v-model.trim="data.nickname" :placeholder="'请填写昵称'" />
    </el-form-item>
     <el-form-item :label="'手机号'" prop="mobile">
      <el-input v-model.trim="data.mobile" :placeholder="'请填写手机号'" />
    </el-form-item>
       <el-form-item :label="'openid'" >
      <el-input v-model.trim="data.openid" :placeholder="'请填写openid'" />
    </el-form-item>
    <el-form-item :label="'头像'" v-loading="loading">
          <el-upload
              :action="$store.state.settings.file_upload_url"
              :headers ="headerObj"
              :limit=1
              list-type="picture-card"
              accept="image/png, image/jpeg, image/gif, image/jpg"
              :on-success="handleavatarSuccess"
              :before-upload="beforeavatarUpload"
              :on-exceed="handleavatarExceed"
              :on-preview="handleavatarPreview"
              :file-list="data.avatarfileList"
              :on-remove="handleavatarRemove">
                <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible" append-to-body width="30%">
                <img width="100%" :src="previewUrl" alt="">
          </el-dialog>
    </el-form-item>
  

    <el-form-item>
      <el-button type="primary"   icon="el-icon-check" :disabled="$store.state.settings.ajaxButton" @click="submit">提交</el-button>
    </el-form-item>
  </el-form>
</template>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 120px;
    height: 120px;
    line-height: 120px;
    text-align: center;
  }
  .avatar {
    width: 120px;
    height: 120px;
    display: block;
  }
    .el-upload-list__item {
         transition: none !important;
  }
</style>
<script>
import { edit,detail } from '@/api/user_list'
import { getToken } from '@/utils/auth'



export default {
  name: 'userEdit',
  props: {
    item: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      loading:true,
      dialogVisible:false,
      previewUrl:'',
      headerObj: {
        'Authorization': 'Bearer ' + getToken()
        },
    rules: {
        mobile: [
          { required: true, message: '手机不能为空', trigger: 'blur' }
        ],
        nickname: [
          { required: true, message: '昵称不能为空', trigger: 'blur' }
        ]
      },
      data: {
             mobile: '',
            nickname: '',
            avatar: '', 
            avatarfileList: [],
            introduce: '',
            info :'',
            openid:''
         }
    }
  },
  created() {
    const id = this.item.id
    this.id = id
    this.fetchData(id)
  },
  watch: {
    item: {
      handler(val, oldVal) {
        if (this.item.dialogVisible === true 
        ) {
          this.id = val.id
          this.fetchData(val.id)
        }
      },
      deep: true
    }
  },
  methods: {

            handleavatarExceed(){ 
                this.$message.error('只能上传一张图片');
             },
            handleavatarPreview(file){
                 this.dialogVisible = true;
                 this.previewUrl = file.url;
             },
            handleavatarRemove(){
                 this.data.avatar = '';
             },
             handleavatarSuccess(response, file, fileList) {
                this.$store.commit('settings/SET_AJAX_BUTTON', false);
                this.data.avatar= response.data.src;
            },
             beforedtUpload(file) {
                this.$store.commit('settings/SET_AJAX_BUTTON', true);
                const isJPG = file.type === "image/jpeg";
                const isPNG = file.type === "image/png";
                const isLt2M = file.size / 1024 / 1024 < 2;
                if (!isJPG && !isPNG) {
                this.$message.error("上传头像图片只能是 JPG 、PNG格式!");
                }
                if (!isLt2M) {
                this.$message.error("上传头像图片大小不能超过 2MB!");
                }
                return isJPG && isLt2M;
            },
     
            handleavatarSuccess(response, file, fileList) {
                    this.$store.commit('settings/SET_AJAX_BUTTON', false);
                this.data.avatar =  response.data.src
                this.$message.success('上传成功');
            },
           beforeavatarUpload(file) {
                    this.$store.commit('settings/SET_AJAX_BUTTON', true);
                    const isJPG = file.type === 'image/jpeg';
                    const isPNG = file.type === 'image/png';
                    const isLt2M = file.size / 1024 / 1024 < 2;

                    if (!isJPG && !isPNG) {
                    this.$message.error('上传头像图片只能是 JPG 、PNG格式!');
                    this.loading =false;
                    }
                    if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                    this.loading =false;
                    }
                },
     


    fetchData(id) {
          detail(id).then(response => {
            this.loading = false;
            this.data = response.data;
            this.data.avatarfileList=response.data.avatar ? response.data.avatar.split(',').map(item=>{return{name:item,url:item}}):[];

          }).catch(err => {
            console.log(err)
          })
        },
    submit() {
      const thiz = this
      this.$refs.form.validate(valid => {
        if (!valid) {
          return false
        }
        edit(this.data).then(response => {
          this.$store.commit('settings/SET_AJAX_BUTTON', true);
          this.$message({
            message: '编辑成功',
            type: 'success',
            duration: 1500,
            onClose() {
              if (thiz.$refs.form !== undefined) {
                thiz.$refs.form.resetFields()
              }
              thiz.item.dialogVisible = false
            }
          })
        })
      })
    }
  }
}
</script>
